#include "los_queue.h"
#include "los_adapter.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>


//创建队列
bool test_queue_create()
{
	UINT32 queueID;
	UINT32 result;
	//创建队列，返回0创建成功
	result = LOS_QueueCreate("testMSG",20,&queueID,0,64);	
	printf("result=%x\n",result);
	return true;	
}

//往队列中写入一条消息并从队列读出一条消息
bool test_queue_write_read()
{
	UINT32 queueID;
	//创建队列，返回0创建成功
	UINT32 result = LOS_QueueCreate("testMSG",1,&queueID,0,64);
	//printf("result=%x\n",result);
	char bufferAddr[] = "helloQueue1!";
	UINT32 bufferSize = sizeof(bufferAddr);
	if(result==0)
	{
		//往队列中写入一条消息
		UINT32 result2 = LOS_QueueWriteCopy(queueID,bufferAddr,bufferSize,1000);
		//printf("result2=%x\n",result);
	}
	
	//从队列中读出一条消息
	char readBufferAddr[128];
	UINT32 result3 = LOS_QueueReadCopy(queueID,readBufferAddr,&bufferSize,1000);
	//printf("result3=%x\n",result);
	printf("readBufferAddr = %s\n",readBufferAddr);
 
}

//往队列中写入多条消息读出全部信息
bool test_queue_writes_reads()
{
	UINT32 queueID;
	//创建队列，返回0创建成功
	UINT32 result = LOS_QueueCreate("testMSG",5,&queueID,0,128);
	char bufferAddr1[] = "helloQueue1!";
	char bufferAddr2[] = "helloQueue2!";
	char bufferAddr3[] = "helloQueue3!";
	
	//往队列中写入3条信息
	if(result == 0)
	{
		LOS_QueueWriteCopy(queueID,bufferAddr1,sizeof(bufferAddr1),1000);
		LOS_QueueWriteCopy(queueID,bufferAddr2,sizeof(bufferAddr2),1000);
		LOS_QueueWriteCopy(queueID,bufferAddr3,sizeof(bufferAddr3),1000);
	}
	//从队列中读出所有信息
	char readBufferAddr[64];
	UINT32 bufferSize = sizeof(readBufferAddr);
	
	//获取队列中可读信息个数
	QUEUE_INFO_S queueInfo;
	LOS_QueueInfoGet(queueID,&queueInfo);
	UINT16 readableCnt = queueInfo.readableCnt;
	printf("readableCnt = %d\n",readableCnt);
	//读出所有信息
	for(int i=0;i<readableCnt;i++)
	{
		LOS_QueueReadCopy(queueID,readBufferAddr,&bufferSize,1000);
		printf("readBufferAddr = %s\n",readBufferAddr);
	}
	
}

int main()
{
	//初始化
	OsQueueInit();
	//创建队列
	//test_queue_create();
	//往队列中写入一条消息并从队列读出一条消息
	//test_queue_write_read();
	//往队列中写入多条消息读出全部信息
	test_queue_writes_reads();
	
	return 0;
}